﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace Tank
{
    class ExplosionEmitter
    {
        List<ParticleEmitter> emitterList;
        // Animation fireAnimation;
        Vector2 position;

        public ExplosionEmitter(Vector2 pos)
        {
            position = new Vector2(pos.X, pos.Y);
            emitterList = new List<ParticleEmitter>();

            CreateEmitters();
        }

        private void CreateEmitters()
        {
            // Base emitter for overall tank smoke
            Random rand = new Random();
            
            Vector2 dir = new Vector2((float)rand.NextDouble(), (float)rand.NextDouble());
            dir.Normalize();
            ParticleEmitter baseEmitter = new ParticleEmitter(20, position, new Vector2(), dir, 20.0f, 0, 3.0f, 0, 0.7f, new Color(50,50,50), 2.0f, 0, 1, 0.4f, 2);
            baseEmitter.SetTexture("dust", new Rectangle(0, 0, 31, 31));
            baseEmitter.SetFadeStyle(ParticleFadeStyle.EXPAND);
            baseEmitter.SetFadeSize(0.2f, 2.0f);
            baseEmitter.EmitterLifeTime = 7.0f;
            emitterList.Add(baseEmitter);


            Vector2 newDir = CreateRandomDirection(12);
            float velMag = 20.0f;
            Vector2 newVel = new Vector2(velMag * newDir.X, velMag * newDir.Y);
            ParticleEmitter ejection = new ParticleEmitter(20, position, newVel, newDir, 1.0f, 0, 0.3f, 1.3f, 0.8f, Color.Gray, 3.0f, 0, 1, 0.3f, 2);
            ejection.SetTexture("dust", new Rectangle(0, 0, 31, 31));
            ejection.SetFadeStyle(ParticleFadeStyle.EXPAND);
            ejection.SetFadeSize(0.1f, 1.5f);
            ejection.EmitterLifeTime = 4.5f;
            emitterList.Add(ejection);

            Vector2 newDir2 = CreateRandomDirection(80);
            velMag = 25.0f;
            Vector2 newVel2 = new Vector2(velMag * newDir2.X, velMag * newDir2.Y);
            ParticleEmitter ejection2 = new ParticleEmitter(20, position, newVel2, newDir2, 1.0f, 0, 0.3f, 1.3f, 0.8f, Color.Gray, 3.0f, 0, 1, 0.3f, 2);
            ejection2.SetTexture("dust", new Rectangle(0, 0, 31, 31));
            ejection2.SetFadeStyle(ParticleFadeStyle.EXPAND);
            ejection2.SetFadeSize(0.1f, 1.3f);
            ejection2.EmitterLifeTime = 5.0f;
            emitterList.Add(ejection2);

            Vector2 newDir3 = CreateRandomDirection(51);
            velMag = 20.0f;
            Vector2 newVel3 = new Vector2(velMag * newDir3.X, velMag * newDir3.Y);
            ParticleEmitter ejection3 = new ParticleEmitter(20, position, newVel3, newDir3, 1.0f, 0, 0.3f, 1.2f, 0.8f, Color.Gray, 3.0f, 0, 1, 0.3f, 2);
            ejection3.SetTexture("dust", new Rectangle(0, 0, 31, 31));
            ejection3.SetFadeStyle(ParticleFadeStyle.EXPAND);
            ejection3.SetFadeSize(0.1f, 1.4f);
            ejection3.EmitterLifeTime = 4.0f;
            emitterList.Add(ejection3);

        }

        public Vector2 CreateRandomDirection(int seed)
        {
            Vector2 dir = new Vector2();
            Random rand = new Random(seed);
            dir.X = rand.Next(-100, 100);
            dir.Y = rand.Next(-100, 100);
            dir.X *= 0.01f;
            dir.Y *= 0.01f;
            dir.Normalize();
            return dir;
        }

        public void Update(float deltaTime)
        { 
            foreach(ParticleEmitter emitter in emitterList)
            {
                emitter.Update(deltaTime);
            }
        }

        public void Draw(SpriteBatch sb)
        {
            foreach (ParticleEmitter emitter in emitterList)
            {
                emitter.Draw(sb);
            }
        }

        public Vector2 Position
        {
            set
            {
                position = value;
                foreach (ParticleEmitter emitter in emitterList)
                {
                    emitter.Position = value;
                }
            }
        }
    }
}
